package NG::Gen::Users::Auth::User;
use strict;
use warnings;
use utf8;

use NG::Sys::Datetime;

sub create_auth {
	my $self = shift;
	my $auth_type = shift;
	my $auth_key = shift;
	my $auth_data = shift;
	if($self->{parent}->auth->get_by_auth($self->{parent},$auth_type,$auth_key)){
		return "Такой ключ '$auth_key' уже сть";
	}
	if(@{$self->auth->get_by_key($auth_type)}){
		return "Такой тип '$auth_type' уже есть";
	}
	$self->auth->insert({
		auth_type => $auth_type,
		auth_key => $auth_key,
		auth_data => $auth_data,
		date_create => unixtime2timestamp(),
	});
	return 0;
}
sub find_auth {
	my $self = shift;
	my $auth_type = shift;
	my $auth = $self->auth->get_by_key($auth_type,1);
	unless($auth && @$auth){
		return "Нет такого типа '$auth_type'";
	}
	return $auth->[0]->auth_object;
}
sub check_auth {
	my $self = shift;
	my $auth_type = shift;
	my $auth_data = shift;
	my $auth = $self->find_auth($auth_type);
	return $auth->check($auth_data);
}
1;
